package com.sky.happygos.ui.activity.adapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;
import com.sky.happygos.R;
import com.sky.happygos.base.ImagePath;
import com.sky.happygos.common.MyListView;
import com.sky.happygos.model.Order;
import com.sky.happygos.model.OrderGoods;
import com.sky.happygos.utils.DateUtil;
import com.sky.happygos.utils.DensityUtil;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qqqqqqq on 2017/4/14.
 */

public class TransAdapter extends RecyclerView.Adapter{
    private static final int FOOTER = 1;
    private static final int NOMAL = 0;

    public Context context;
    public LayoutInflater layoutInflater;
    public List<Order> list;
    public MyItemClickListener myItemClickListener;

    public interface MyItemClickListener {
        void onItemClick(View view, int postion);
        void finishOrderClick(int position);
        void failOrderClick(int position);
        void receiveOrderClick(int position);

    }


    public TransAdapter(Context context, List<Order> list){
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        this.list = list;
    }

    public void setMyItemClickListener(MyItemClickListener myItemClickListener){
        this.myItemClickListener = myItemClickListener;
    }



    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == NOMAL) {
            return new TransHolder(layoutInflater.inflate(R.layout.item_trans, null),myItemClickListener);
        }
        if(viewType == FOOTER){
            return new FooterHolder(layoutInflater.inflate(R.layout.item_load_footer, null));
        }
        return null;
    }



    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(position == list.size()){
            return;
        }
        if(holder instanceof TransHolder) {
            ((TransHolder) holder).bindData(list.get(position));
        }
    }



    @Override
    public int getItemViewType(int position) {
        if(position == list.size()){
            return FOOTER;
        }
        return NOMAL;
    }

    @Override
    public int getItemCount() {
        return list.size()+1;
    }

    class TransHolder extends RecyclerView.ViewHolder{
        SimpleDraweeView img;

        TextView userName;
        TextView status;
//        TextView goodsCount;
        TextView allPrice;
        TextView finishOrder;
        TextView failOrder;
        TextView receiveOrder;
        TextView phone;
        TextView addr;
        TextView time;
        LinearLayout goodsContainer;
        List<OrderGoodsHolder> views;
        OrderGoodsAdapter orderGoodsAdapter;


        MyItemClickListener myItemClickListenerr;

        Order order;

        public TransHolder(View itemView, final MyItemClickListener myItemClickListener) {
            super(itemView);
            this.myItemClickListenerr = myItemClickListener;
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myItemClickListenerr.onItemClick(v,getPosition());
                }
            });
            userName = (TextView) itemView.findViewById(R.id.tv_trans_user_name);
            phone = (TextView) itemView.findViewById(R.id.tv_trans_user_phone);
            addr = (TextView) itemView.findViewById(R.id.tv_trans_user_addr);
            status = (TextView) itemView.findViewById(R.id.tv_trans_order_status);
//            goodsCount = (TextView) itemView.findViewById(R.id.tv_trans_goods_count);
            allPrice = (TextView) itemView.findViewById(R.id.tv_trans_goods_allpricce);
            goodsContainer = (LinearLayout) itemView.findViewById(R.id.ll_order_goods_container);
            time = (TextView) itemView.findViewById(R.id.tv_trans_order_time);
            orderGoodsAdapter = new OrderGoodsAdapter(new ArrayList<OrderGoods>());
            finishOrder = (TextView) itemView.findViewById(R.id.tv_finish_order);
            finishOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myItemClickListener.finishOrderClick(getPosition());
                }
            });
            failOrder = (TextView) itemView.findViewById(R.id.tv_fail_order);
            failOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myItemClickListener.failOrderClick(getPosition());
                }
            });
            receiveOrder = (TextView) itemView.findViewById(R.id.tv_recv_order);
            receiveOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myItemClickListener.receiveOrderClick(getPosition());
                }
            });

        }



        public void bindData(Order order){
            this.order = order;
            if(views == null){
                views = new ArrayList<>();
            }
            if(order.user.name != null && order.user.name.length() > 0 ){
                userName.setText(order.user.name);
            }else{
                userName.setText(order.user.phone);
            }

            if(order.addr != null) {
                phone.setText(String.format("联系方式:%s",order.addr.phone));
                addr.setText(String.format("详细地址:%s", order.addr.detail));
            }else{
                phone.setText(String.format("联系方式:%s",order.user.phone));
                addr.setText(String.format("详细地址:%s", " "));
            }
            if(order.status == 0){
                status.setText("等待商家接受订单");
                finishOrder.setVisibility(View.GONE);
                failOrder.setVisibility(View.GONE);
                receiveOrder.setVisibility(View.VISIBLE);
            }
            if(order.status == 1){
                status.setText("交易完成");
                failOrder.setVisibility(View.VISIBLE);
                finishOrder.setVisibility(View.GONE);
                receiveOrder.setVisibility(View.INVISIBLE);
            }
            if(order.status == 2){
                status.setText("交易失败");
                failOrder.setVisibility(View.GONE);
                finishOrder.setVisibility(View.GONE);
                receiveOrder.setVisibility(View.GONE);
            }
            if(order.status == 3){
                status.setText("订单已接受");
                failOrder.setVisibility(View.VISIBLE);
                finishOrder.setVisibility(View.VISIBLE);
                receiveOrder.setVisibility(View.INVISIBLE);
            }
            time.setText(DateUtil.timeStamp2Date(order.begin+"",null));
//            goodsCount.setText(String.format("共有%d件商品",order.goods.size()));
            allPrice.setText(String.format("总价：%.2f",(order.price)));

            if(views.size() == order.goods.size()){
                for(int i = 0;i < views.size();i++){
                    views.get(i).bindData(order.goods.get(i));
                }
            }else{
                views.clear();
                goodsContainer.removeAllViews();
                for(OrderGoods orderGoods : order.goods){
                    OrderGoodsHolder orderGoodsHolder = new OrderGoodsHolder(R.layout.item_trans_goods);
                    orderGoodsHolder.bindData(orderGoods);
                    views.add(orderGoodsHolder);
                    goodsContainer.addView(orderGoodsHolder.view);
                }
            }

        }
    }
    class FooterHolder extends RecyclerView.ViewHolder{

        public FooterHolder(View itemView) {
            super(itemView);
        }

        public void bindData(){

        }
    }

    class OrderGoodsHolder{
        View view;
        SimpleDraweeView simpleDraweeView;
        TextView name;
        TextView price;
        TextView count;

        OrderGoods orderGoods;

        public OrderGoodsHolder(@LayoutRes int resource){
            this.view = LayoutInflater.from(context).inflate(resource,null);
            this.simpleDraweeView = (SimpleDraweeView) view.findViewById(R.id.sdv_trans_goods_icon);
            this.name = (TextView) view.findViewById(R.id.tv_trans_goods_name);
            this.price = (TextView) view.findViewById(R.id.tv_trans_goods_price);
            this.count = (TextView) view.findViewById(R.id.tv_trans_goods_count);
        }
        public void bindData(OrderGoods orderGoods){
            this.orderGoods = orderGoods;
            if(orderGoods.goodsId != null){
                simpleDraweeView.setImageURI(ImagePath.to(orderGoods.goodsId.icon));
                name.setText(orderGoods.goodsId.name);
                price.setText(String.format("¥ %.2f",orderGoods.goodsId.price));
            }
            count.setText(String.format("× %d",orderGoods.count));
        }
    }

    class OrderGoodsAdapter extends BaseAdapter{
        List<OrderGoods> orderGoodss;

        public OrderGoodsAdapter(List<OrderGoods> goods){
            this.orderGoodss = goods;
        }

        @Override
        public int getCount() {
            return orderGoodss.size();
        }

        @Override
        public OrderGoods getItem(int position) {
            return orderGoodss.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            OrderGoods orderGoods = getItem(position);
            View view = LayoutInflater.from(context).inflate(R.layout.item_trans_goods,null);
            SimpleDraweeView simpleDraweeView = (SimpleDraweeView) view.findViewById(R.id.sdv_trans_goods_icon);
            simpleDraweeView.setImageURI(ImagePath.to(orderGoods.goodsId.icon));
            TextView name = (TextView) view.findViewById(R.id.tv_trans_goods_name);
            name.setText(orderGoods.goodsId.name);
            TextView price = (TextView) view.findViewById(R.id.tv_trans_goods_price);
            price.setText(String.format("¥ %.2f",orderGoods.goodsId.price));
            TextView count = (TextView) view.findViewById(R.id.tv_trans_goods_count);
            count.setText(String.format("× %d",orderGoods.count));
            return view;
        }
    }
}
